//
//  JWMovieDetailCommentCell.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/21.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWMovieDetailCommentCell.h"
#import "JWMovieDetailCommentModel.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
#import "constant.h"

@interface JWMovieDetailCommentCell ()
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *content;
@property (nonatomic, strong) UILabel *nickname;
@property (nonatomic, strong) UILabel *rate;
@property (nonatomic, strong) UIView *commentView;

@end

CGFloat JWSpacing = 10;   // 统一间距

@implementation JWMovieDetailCommentCell

- (void)awakeFromNib {
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView *imageVeiw = [[UIImageView alloc] initWithFrame:CGRectZero];
        imageVeiw.layer.cornerRadius = 5.f;
        imageVeiw.layer.masksToBounds = YES;
        [self.contentView addSubview:imageVeiw];
        _icon = imageVeiw;
        
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        contentLabel.font = [UIFont systemFontOfSize:15];
        contentLabel.numberOfLines = 0;
//        contentLabel.backgroundColor = [UIColor greenColor];
        _content = contentLabel;
        
        UILabel *nicknameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        nicknameLabel.textColor = [UIColor orangeColor];
        nicknameLabel.font = [UIFont systemFontOfSize:15];
//        nicknameLabel.backgroundColor = [UIColor redColor];
        _nickname = nicknameLabel;
        
        UILabel *ratingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        ratingLabel.textColor = [UIColor grayColor];
        ratingLabel.contentMode = UIViewContentModeBottomRight;
        ratingLabel.font = [UIFont systemFontOfSize:15];
//        ratingLabel.backgroundColor = [UIColor blueColor];
        
        _rate = ratingLabel;
        
        _commentView = [[UIView alloc] initWithFrame:CGRectZero];
        _commentView.backgroundColor = [UIColor whiteColor];
        _commentView.layer.cornerRadius = 8.f;
        
        [_commentView addSubview:contentLabel];
        [_commentView addSubview:nicknameLabel];
        [_commentView addSubview:ratingLabel];
        
        [self.contentView addSubview:_commentView];
        
        
     
    }
    
    return  self;
}
- (void)setMovieDetailCommentModel:(JWMovieDetailCommentModel *)movieDetailCommentModel{
    
    _movieDetailCommentModel = movieDetailCommentModel;
    NSString *urlStr = _movieDetailCommentModel.userImage;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"sctpxw@2x"]];
    
    self.content.text = _movieDetailCommentModel.content;
    
    self.nickname.text = _movieDetailCommentModel.nickname;
    
    self.rate.text = _movieDetailCommentModel.rating;
    
    self.cellHeight = _movieDetailCommentModel.cellHeight;
  
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.icon.frame = CGRectMake(JWSpacing, JWSpacing, 60, 60);
    

    /* -------------------------设置展开和为展开的高度--------------------------------------- */
    
    CGFloat commentViewWidth = JWScreenWidth - 60 - JWSpacing * 2 - JWSpacing;      //  底部容器视图的宽
    
    self.nickname.frame = CGRectMake(JWSpacing, JWSpacing, 200, 20);
    self.rate.frame = CGRectMake(commentViewWidth - JWSpacing - 30, JWSpacing, 30, 20);
    
    CGSize maxSizi = CGSizeMake(commentViewWidth - JWSpacing * 2, MAXFLOAT);
    
    CGSize realSize = [self.content.text boundingRectWithSize:maxSizi
                                                      options:NSStringDrawingUsesLineFragmentOrigin
                                                   attributes:@{
                                                                NSFontAttributeName : [UIFont fontWithName:@"黑体-简" size:17]
                                                                }
                                                      context:nil].size;
    
    // 文字的frame
    CGSize textSize = [self.content.text sizeWithAttributes:@{ NSFontAttributeName : [UIFont fontWithName:@"黑体-简" size:17]}];
    CGFloat commentViewHeigh = realSize.height + JWSpacing * 2 + self.nickname.frame.size.height;
    
    /*---------是否展开-----------*/
    
    if (_movieDetailCommentModel.open && ((commentViewHeigh + JWSpacing * 2) > 85)) {//比原来的大才展开
        
        self.content.frame = CGRectMake(JWSpacing, self.nickname.frame.size.height + JWSpacing, commentViewWidth - JWSpacing * 2, realSize.height);
        self.commentView.frame = CGRectMake(self.icon.frame.size.width + JWSpacing * 2 , JWSpacing, commentViewWidth,  commentViewHeigh);
        
    }else {
        
        self.content.frame = CGRectMake(JWSpacing,self.nickname.frame.size.height + JWSpacing,commentViewWidth - JWSpacing * 2, textSize.height + 10);
        self.commentView.frame = CGRectMake(self.icon.frame.size.width + JWSpacing * 2, JWSpacing, commentViewWidth, self.nickname.frame.size.height + self.content.frame.size.height + JWSpacing * 2 );       // 内外
        
    }
    
    

}


+ (instancetype)movieDetailCommentModelWithTableView:(UITableView *)tableView{
    static NSString * const ID = @"commentCell";
    JWMovieDetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[JWMovieDetailCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
